//
//  ScenicStrategyVC.m
//  ImGuider
//
//  Created by 王鹏宇 on 2017/9/8.
//  Copyright © 2017年 AudioCtrip. All rights reserved.
//

#import "ScenicStrategyVC.h"
#import <WebKit/WebKit.h>
#import "GuiderDetailVC.h"
@interface ScenicStrategyVC ()<WKUIDelegate, WKNavigationDelegate, WKScriptMessageHandler,UIScrollViewDelegate>

@property (nonatomic, strong) WKWebView *webView;


@end

@implementation ScenicStrategyVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setup];
}


- (void)setup {
    
    self.barBGAlpha = 0;
    
    WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
    [config.userContentController addScriptMessageHandler:self name:@"imguider"];
    self.webView = [[WKWebView alloc] initWithFrame:CGRectMake(0,0, kScreenWidth, kScreenHeight - 126 - kBottomHeight) configuration:config];
    
    self.webView.UIDelegate = self;
    self.webView.navigationDelegate = self;
    
    [self.view addSubview:self.webView];
    
    self.subScrollView = self.webView.scrollView;
    self.subScrollView.delegate = self;
    if (self.url.length == 0) {
        
        [IGUtil showToast:MYLocalizedString(@"HOMEPAGE_STRATEGY_UNKONWN", nil)];
        return;
    }
    
    [ActivityIndicatorView showHUDAddedTo:self.webView];
    
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.url]]];
    
    
    
}


#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat offsetY = scrollView.contentOffset.y;
    if (!self.canScroll) {
        [scrollView setContentOffset:CGPointZero];
    }
    
    if (offsetY < 0 ) {
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"kLeaveTopNtf" object:@1];
        self.canScroll = NO;
        scrollView.contentOffset = CGPointZero;
    }
}

- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
    
    NSDictionary *obj = message.body;
    
    NSString *strSel = obj[@"method"];
    NSDictionary *param = obj[@"params"];
    
    if (strSel && param.count > 0) {
        
        strSel = [strSel stringByAppendingString:@":"];
    }
    
    SEL sel = NSSelectorFromString(strSel);
    
    if (sel && [self respondsToSelector:sel]) {
        
        [self performSelector:sel withObject:param];
    }
    
}

- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation {
    
    // 禁用选中效果
    [self.webView evaluateJavaScript:@"document.documentElement.style.webkitUserSelect='none'" completionHandler:nil];
    [self.webView evaluateJavaScript:@"document.documentElement.style.webkitTouchCallout='none'" completionHandler:nil];
    
}


- (void)webView:(WKWebView *)webView didCommitNavigation:(null_unspecified WKNavigation *)navigation {
    
        [ActivityIndicatorView hideHUDAddedTo:self.webView];
    
}

- (void)webView:(WKWebView *)webView didFailNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error {
    
    [ActivityIndicatorView hideHUDAddedTo:self.webView];
}



#pragma mark -webinterface

- (void)jumpguider:(NSDictionary *)params {
    
    NSString *guiderid = params[@"guiderid"];
    
    GuiderDetailVC * vc = [GuiderDetailVC new];
    vc.guiderID = guiderid;
    
    [self.superVC.navigationController pushViewController:vc animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
